//
//  JPAES.m
//  xychain_DM_iOS
//
//  Created by Jpwang on 2018/5/24.
//  Copyright © 2018年 Jpwang. All rights reserved.
//

#import "JPAES.h"

@implementation JPAES

+ (NSData *)random128BitAESKey {
    unsigned char buf[16];
    arc4random_buf(buf, sizeof(buf));
    return [NSData dataWithBytes:buf length:sizeof(buf)];
}

/*加密方法*/
+ (NSString *)AES128EncryptWithPlainText:(NSString *)plain pwdString:(NSString *)password {
    NSData *data = [[NSData alloc] initWithBase64EncodedString:password options:NSDataBase64DecodingIgnoreUnknownCharacters];
    return [JPAES AES128EncryptWithPlainText:plain pwdData:data];
}

/*加密方法*/
+ (NSString *)AES128EncryptWithPlainText:(NSString *)plain pwdData:(NSData *)data {
    NSData *plainText = [plain dataUsingEncoding:NSUTF8StringEncoding];
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise
    // char keyPtr[kCCKeySizeAES128+1]; // room for terminator (unused)
    // bzero(keyPtr, sizeof(keyPtr));   // fill with zeroes (for padding)
    NSUInteger dataLength = [plainText length];
    void const *keyBytes = data.bytes;
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    bzero(buffer, sizeof(buffer));
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES, kCCOptionPKCS7Padding | kCCOptionECBMode,
                                          keyBytes, kCCKeySizeAES128,
                                          NULL /* initialization vector (optional) */,
                                          [plainText bytes], dataLength, /* input */
                                          buffer, bufferSize, /* output */
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        NSData *encryptData = [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
        return [encryptData base64Encoding];
    }
    free(buffer); //free the buffer;
    return nil;
}

/*解密方法*/
+ (NSString *)AES128DecryptWithCiphertext:(NSString *)ciphertexts pwdString:(NSString *)password {
    NSData *data = [[NSData alloc] initWithBase64EncodedString:password options:NSDataBase64DecodingIgnoreUnknownCharacters];
    return [JPAES AES128DecryptWithCiphertext:ciphertexts pwdData:data];
}

/*解密方法*/
+ (NSString *)AES128DecryptWithCiphertext:(NSString *)ciphertexts pwdData:(NSData *)data {
    NSData *cipherData = [[NSData alloc] initWithBase64EncodedString:ciphertexts options:NSDataBase64DecodingIgnoreUnknownCharacters];
    // NSData *cipherData = [NSData dataWithBase64EncodedString:ciphertexts];
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise
    // char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
    // bzero(keyPtr, sizeof(keyPtr));   // fill with zeroes (for padding)
    NSUInteger dataLength = [cipherData length];
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    size_t numBytesDecrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding | kCCOptionECBMode,
                                          [data bytes], kCCKeySizeAES128,
                                          NULL ,/* initialization vector (optional) */
                                          [cipherData bytes], dataLength, /* input */
                                          buffer, bufferSize, /* output */
                                          &numBytesDecrypted);
    
    if (cryptStatus == kCCSuccess) {
        NSData *encryptData = [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
        return [[[NSString alloc] initWithData:encryptData encoding:NSUTF8StringEncoding] init];
    }
    free(buffer); //free the buffer;
    return nil;
}

@end
